package thread;

import java.util.concurrent.*;


class MyThreadPool1 {

    //阻塞队列
    private BlockingQueue<Runnable> blockingQueue = null;

    public MyThreadPool1(int n) {
        //容量为1000的阻塞队列
        blockingQueue = new ArrayBlockingQueue(1000);
        //创建n个线程
        for (int i = 0; i < n; i++) {
            //每个线程都不断的向队列中拿出任务
            Thread t = new Thread(() -> {
                while (true) {
                    try {
                        //取出队列中的任务
                        Runnable r = blockingQueue.take();
                        //执行
                        r.run();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            t.start();
        }
    }

    public void submit(Runnable r) throws InterruptedException {
        blockingQueue.put(r);
    }

}


public class Demo60 {
    public static void main(String[] args) throws InterruptedException {
        //4个线程的线程池
        MyThreadPool1 threadPool1 = new MyThreadPool1(4);
        //往线程池中提交1000个任务
        for (int i = 1; i <= 1000; i++) {
            int id = i;
            threadPool1.submit(() -> {
                System.out.println(Thread.currentThread().getName()+"id："+id);
            });
        }
    }
}
